package battleship.setup;

import java.util.Random;

import battleship.Controller;
import battleship.PluginSetup;
import battleship.boat.Boat;
import battleship.field.Field;
import battleship.player.Player;
import battleship.view.Views;

public class SetRandom implements PluginSetup {

	@Override
	public String setupGetFunction() {
		return "random";
	}

	@Override
	public String setupGetDescription() {
		
		return "stelt je boten automatisch in";
	}

	@Override
	public String setupGetOptions() {
		return "geen opties";
	}

	@Override
	public int setupExecFunction(Player player, Boat boat, String[] arguments) {
		
		boolean goodPosition = false;
		
		while (!goodPosition)
		{
		
		Random randomX = new Random();
		Random randomY = new Random();
		Random vertical = new Random();
		
		int x = Math.abs(randomX.nextInt() % Field.getWidth());
		int y = Math.abs(randomY.nextInt() % Field.getHeight());
		boolean vert = vertical.nextBoolean();
		
		
		
		
			goodPosition = boat.setPosition(player, x, y, vert);
			
		}
		return 0;
	}

	@Override
	public boolean allAtOnce() {
		
		return true;
	}

	@Override
	public boolean setupErrorHandler(int error) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void onFinish(Player player) {
		if(!player.isBot()) // a bot couldnt answer this
		{	
			boolean showField = Controller.getConsole().scanYesNo("Wil je je volledige veld zien?");
			
			if(showField)
			{
				Controller.getController().printBoard(true, player.getField());
			}	
		}
	}

	@Override
	public Views[] setupSupportedViews() {
		
		return (new Views[] {Views.Console, Views.GUI});
	}

}
